{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-15T07:34:25.116831Z",
     "start_time": "2025-09-15T07:34:25.113494Z"
    }
   },
   "source": [
    "import functools\n",
    "import time\n",
    "\n",
    "\n",
    "# def my_decorator(func):\n",
    "#     def wrapper(*args, **kwargs):\n",
    "#         print('wrapper of decorator')\n",
    "#         func(*args, **kwargs)\n",
    "#     return wrapper\n",
    "#\n",
    "# @my_decorator\n",
    "# def greet(message):\n",
    "#     print(message)\n",
    "#\n",
    "# greet('hello world')\n",
    "# 函数装饰器\n",
    "# def repeat(num):\n",
    "#     def my_decorator(func):\n",
    "#         @functools.wraps(func)\n",
    "#         def wrapper(*args, **kwargs):\n",
    "#             for i in range(num):\n",
    "#                 print('wrapper of decorator')\n",
    "#                 func(*args, **kwargs)\n",
    "#         return wrapper\n",
    "#     return my_decorator\n",
    "#\n",
    "# @repeat(3)\n",
    "# def greet(message):\n",
    "#     print(message)\n",
    "# greet('hello world')\n",
    "#\n",
    "# greet.__name__\n",
    "# 类装饰器\n",
    "# class Count:\n",
    "#     def __init__(self,func):\n",
    "#         self.func = func\n",
    "#         self.num_calls = 0\n",
    "#\n",
    "#     def __call__(self, *args, **kwargs):\n",
    "#         self.num_calls += 1\n",
    "#         print('num of calls is:{}'.format(self.num_calls))\n",
    "#         return self.func(*args, **kwargs)\n",
    "#\n",
    "# @Count\n",
    "# def example():\n",
    "#     print('hello world')\n",
    "# example()\n",
    "# 多个装饰器\n",
    "# def my_decorator1(func):\n",
    "#     @functools.wraps(func)\n",
    "#     def wrapper(*args, **kwargs):\n",
    "#         print('execute decorator1')\n",
    "#         func(*args, **kwargs)\n",
    "#     return wrapper\n",
    "#\n",
    "# def my_decorator2(func):\n",
    "#     @functools.wraps(func)\n",
    "#     def wrapper(*args, **kwargs):\n",
    "#         print('execute decorator2')\n",
    "#         func(*args, **kwargs)\n",
    "#     return wrapper\n",
    "#\n",
    "# @my_decorator2\n",
    "# @my_decorator1\n",
    "# def greet(message):\n",
    "#     print(message)\n",
    "#\n",
    "# greet('hello world')\n",
    "def log_execution_time(func):\n",
    "    @functools.wraps(func)\n",
    "    def wrapper(*args, **kwargs):\n",
    "        start = time.perf_counter()\n",
    "        res = func(*args, **kwargs)\n",
    "        end = time.perf_counter()\n",
    "        print('{} took {} ms'.format(func.__name__, end - start))\n",
    "        return res\n",
    "    return wrapper\n",
    "\n",
    "@log_execution_time\n",
    "def calculate_similarity(items):"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "execute decorator2\n",
      "execute decorator1\n",
      "hello world\n"
     ]
    }
   ],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
